<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="讲师姓名" prop="teacherName">
        <el-input
          v-model="dataForm.teacherName"
          placeholder="讲师姓名"
        ></el-input>
      </el-form-item>
      <el-form-item label="讲师邮箱" prop="teacherEmail">
        <el-input
          v-model="dataForm.teacherEmail"
          placeholder="讲师邮箱"
        ></el-input>
      </el-form-item>

      <el-form-item label="讲师头像" prop="teacherAvatar">
        <img :src="dataForm.teacherAvatar" min-width="70" height="70" />
        <el-upload
          v-model="dataForm.teacherAvatar"
          class="upload-demo"
          action="http://localhost:8888/api/minio/upload"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          :on-success="handleUploadSuccess"
          :file-list="fileList.url"
          list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png文件，且不超过500kb
          </div>
        </el-upload>
      </el-form-item>

      <el-form-item label="讲师封面" prop="teacherHover">
        <img :src="dataForm.teacherHover" min-width="70" height="70" />
        <el-upload
          v-model="dataForm.teacherHover"
          class="upload-demo"
          action="http://localhost:8888/api/minio/upload"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          :on-success="handleUploadSuccess"
          :file-list="fileList.url"
          list-type="picture"
        >
          <el-button size="small" type="primary">点击上传</el-button>
          <div slot="tip" class="el-upload__tip">
            只能上传jpg/png文件，且不超过500kb
          </div>
        </el-upload>
      </el-form-item>

      <el-form-item label="讲师用户名" prop="teacherUsername">
        <el-input
          v-model="dataForm.teacherUsername"
          placeholder="讲师用户名"
        ></el-input>
      </el-form-item>
      <el-form-item label="讲师简介" prop="teacherDescription">
        <el-input
          v-model="dataForm.teacherDescription"
          placeholder="讲师简介"
        ></el-input>
      </el-form-item>

      <el-form-item label="出生日期" prop="birthday">
        <el-date-picker
          v-model="dataForm.birthday"
          type="date"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="性别">
        <el-radio-group v-model="dataForm.teacherGender">
          <el-radio label="男"></el-radio>
          <el-radio label="女"></el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="讲师学历" prop="teacherLevelName">
        <el-select v-model="dataForm.teacherLevelName" placeholder="请选择">
          <el-option
            v-for="item in levelOptions"
            :key="item.id"
            :label="item.teacherLevelName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="讲师类型" prop="teacherTypeName">
        <el-select v-model="dataForm.teacherTypeName" placeholder="请选择">
          <el-option
            v-for="item in typeOptions"
            :key="item.id"
            :label="item.teacherTypeName"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        teacherName: "",
        teacherEmail: "",
        teacherAvatar: "",
        teacherHover: "",
        teacherUsername: "",
        teacherPassword: "",
        teacherDescription: "",
        birthday: "",
        teacherGender: "",
        teacherLevelName: "",
        teacherTypeName: "",
        createTime: "",
        updateTime: "",
        status: "",
        isDelete: "",
      },
      fileList: [
        {
          name: "",
          url: "",
        },
      ],
      levelOptions: [],
      typeOptions: [],
      dataRule: {
        teacherName: [
          { required: true, message: "讲师姓名不能为空", trigger: "blur" },
        ],
        teacherEmail: [
          { required: true, message: "讲师邮箱不能为空", trigger: "blur" },
        ],
        teacherAvatar: [
          { required: false, message: "讲师头像不能为空", trigger: "blur" },
        ],
        teacherHover: [
          { required: false, message: "讲师封面不能为空", trigger: "blur" },
        ],
        teacherUsername: [
          { required: true, message: "讲师用户名不能为空", trigger: "blur" },
        ],
        teacherPassword: [
          { required: false, message: "讲师密码不能为空", trigger: "blur" },
        ],
        teacherDescription: [
          { required: true, message: "讲师简介不能为空", trigger: "blur" },
        ],
        birthday: [
          { required: true, message: "讲师生日不能为空", trigger: "blur" },
        ],
        teacherLevelName: [
          { required: true, message: "讲师学历不能为空", trigger: "blur" },
        ],
        teacherTypeName: [
          { required: true, message: "讲师类型不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: false, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: false, message: "修改时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.getTeacherLevel();
      this.getTeacherType();
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/teacher/teacher/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.teacherName = data.teacher.teacherName;
              this.dataForm.teacherEmail = data.teacher.teacherEmail;
              this.dataForm.teacherAvatar = data.teacher.teacherAvatar;
              this.dataForm.teacherHover = data.teacher.teacherHover;
              this.dataForm.teacherUsername = data.teacher.teacherUsername;
              this.dataForm.teacherPassword = data.teacher.teacherPassword;
              this.dataForm.teacherDescription =
                data.teacher.teacherDescription;
              this.dataForm.birthday = data.teacher.birthday;
              this.dataForm.teacherLevelName = data.teacher.teacherLevelName;
              this.dataForm.teacherTypeName = data.teacher.teacherTypeName;
              this.dataForm.createTime = data.teacher.createTime;
              this.dataForm.updateTime = data.teacher.updateTime;
              this.dataForm.status = data.teacher.status;
              this.dataForm.isDelete = data.teacher.isDelete;
              this.dataForm.teacherGender = data.teacher.teacherGender;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/teacher/teacher/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              teacherName: this.dataForm.teacherName,
              teacherEmail: this.dataForm.teacherEmail,
              teacherAvatar: this.dataForm.teacherAvatar,
              teacherHover: this.dataForm.teacherHover,
              teacherUsername: this.dataForm.teacherUsername,
              teacherPassword: this.dataForm.teacherPassword,
              teacherDescription: this.dataForm.teacherDescription,
              birthday: this.dataForm.birthday,
              teacherLevelName: this.dataForm.teacherLevelName,
              teacherTypeName: this.dataForm.teacherTypeName,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime,
              status: this.dataForm.status,
              isDelete: this.dataForm.isDelete,
              teacherGender: this.dataForm.teacherGender,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
    //获取讲师学历等级数据
    getTeacherLevel() {
      this.$http({
        url: this.$http.adornUrl("/teacher/teacherlevel/findTeacherLevelList"),
        method: "get",
      }).then(({ data }) => {
        if (data && data.code === 0) {
          console.log(data.teacherLevelList);
          this.levelOptions = data.teacherLevelList;
        } else {
          this.levelOptions = [];
        }
      });
    },

    //获取讲师类型数据
    getTeacherType() {
      this.$http({
        url: this.$http.adornUrl("/teacher/teachertype/findTeacherTypeList"),
        method: "get",
      }).then(({ data }) => {
        if (data && data.code === 0) {
          console.log(data.teacherTypeList);
          this.typeOptions = data.teacherTypeList;
        } else {
          this.typeOptions = [];
        }
      });
    },
    handleRemove(file, fileList) {
      console.log(file.response, "1111");
    },
    handlePreview(file) {
      console.log(file);
    },
    handleUploadSuccess(res, file) {
      console.log(file);
      console.log(res);
      console.log("上传成功...");

      this.fileList.push({
        name: file.name,
        url: file.response.msg.replace("${filename}", file.name),
      });

      this.fileList.name = file.name;
      this.fileList.url = file.response.msg;
      console.log(file.response.msg);
      this.dataForm.teacherAvatar = file.response.msg;
      this.dataForm.teacherHover = file.response.msg;
      console.log(this.dataForm.studentAvatar);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}？`);
    },
  },
};
</script>
